package com.ww.springboot.boot.algorithm.leetcode1;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-03-24 11:03
 */
public class AZ661图片平滑器 {

    public static void main(String[] args) {
        int[][] img = {{100,200,100},{200,50,200},{100,200,100}};
        int[][] imgNew = imageSmoother(img);

        PrintUtil.print(imgNew);
    }

    /**
     * 自身及周围8个点的和/9
     * <p>
     * 1.简单遍历
     *
     * @param img
     * @return
     */
    public static int[][] imageSmoother(int[][] img) {
        int[][] result = new int[img.length][img[0].length];

        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[0].length; j++) {
                result[i][j] = getResult(img, i, j);
            }
        }
        return result;
    }

    private static int getResult(int[][] img, int i, int j) {
        int result = 0;
        int newI;
        int newJ;
        int total = 0;
        for (int k = -1; k <= 1; k++) {
            newI = i + k;
            if(newI < 0 || newI >= img.length){
                continue;
            }
            for (int l = -1; l <= 1; l++) {
                newJ = j + l;
                if (newJ >= 0 && newJ < img[0].length) {
                    result += img[newI][newJ];
                    total++;
                }
            }
        }
        result = result / total;
        return result;
    }
}
